package com.example.administrator.dc.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.dc.Bean.EditSchool;
import com.example.administrator.dc.R;
import com.example.administrator.dc.model.UsersModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditSchoolActivity extends AppCompatActivity {


    private EditText userschool;
    private Button confirm;
    String userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_school);
        userschool=(EditText)findViewById(R.id.edit_userschool);
        confirm=(Button)findViewById(R.id.editschool_confirm_btn) ;
        Intent intent=getIntent();
        String school=intent.getStringExtra("school");
        userschool.setText(school);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getUser();
                UsersModel usersModel=new UsersModel();
                Call<EditSchool> call=usersModel.editSchool(userid,userschool.getText().toString());
                call.enqueue(new Callback<EditSchool>() {
                    @Override
                    public void onResponse(Call<EditSchool> call, Response<EditSchool> response) {
                        EditSchool res =response.body();
                        if (!"1".equals(res.getStatus()))
                        {
                            Toast.makeText(EditSchoolActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                          finish();
                        }
                        else
                        {
                            Toast.makeText(EditSchoolActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<EditSchool> call, Throwable t) {

                    }
                });
            }
        });
    }
    private void getUser(){
        SharedPreferences sp = getSharedPreferences("UserInfo", MODE_PRIVATE);
        userid = sp.getString("user_id", null);


    }

}
